<template>
  <div
    class="page_body"
    v-loading.fullscreen.lock="loadingKey"
    :element-loading-text="loadingText"
  >
    <!-- 面包屑 -->
    <el-row>
      <el-col :span="24" class="page_box_top">
        <i class="el-icon-s-fold"></i>
        <el-breadcrumb separator="/" class="page_box_top_nav">
          <el-breadcrumb-item class="breadcrumb_active_item"
            >裁判员</el-breadcrumb-item
          >
          <el-breadcrumb-item>注册年审</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row>
    <el-row>
      <div class="referee_certification_content">
        <!-- 按钮栏 -->
        <el-row>
          <el-col :span="24">
            <div class="referee_certification_top">
              <div style="margin-left: 10px">筛选：</div>
              <el-select
                v-model="checkedLevel"
                placeholder="请选择等级"
                size="mini"
                @change="checkedLevelChange"
                :clearable="true"
                style="margin-left: 10px"
              >
                <el-option
                  v-for="item in levelList"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                >
                </el-option>
              </el-select>

              <el-select
                v-model="checkedStatus"
                placeholder="请选择审核状态"
                size="mini"
                @change="checkedStatusChange"
                :clearable="true"
                style="margin-left: 10px"
              >
                <el-option
                  v-for="item in auditStatusList"
                  :key="item.key"
                  :label="item.value"
                  :value="item.key"
                >
                </el-option>
              </el-select>

              <el-input
                v-model="searchKey"
                placeholder="请输入关键词"
                size="mini"
                style="width: 200px; margin-left: 10px"
              ></el-input>
              <el-button
                type="primary"
                size="mini"
                style="margin-left: 5px"
                @click="searchFn()"
                >搜索</el-button
              >
              <el-button
                type="primary"
                size="mini"
                style="margin-left: 10px"
                @click="exportFn()"
                >导出数据</el-button
              >
            </div>
          </el-col>
        </el-row>
        <!-- 数据列表 -->
        <div
          v-if="
            certificationListData != undefined &&
            certificationListData.length > 0
          "
        >
          <el-row>
            <el-col :span="24">
              <el-table
                :data="certificationListData"
                border
                size="mini"
                :cell-style="cellStyleFn"
                style="width: 100%"
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="50"
                  align="center"
                >
                </el-table-column>

                <el-table-column
                  prop="name"
                  label="姓名"
                  width="120"
                  align="center"
                ></el-table-column>

                <el-table-column label="手机号" width="150" align="center">
                  <template slot-scope="scope">
                    {{ scope.row.phone }}
                  </template>
                </el-table-column>

                <el-table-column
                  prop="level"
                  label="裁判员等级"
                  width="120"
                  align="center"
                  :formatter="formatterLevelFn"
                ></el-table-column>

                <el-table-column
                  prop="year"
                  label="年度"
                  width="100"
                  align="center"
                ></el-table-column>

                <el-table-column
                  prop="created_at"
                  label="注册时间"
                  min-width="150"
                  align="center"
                ></el-table-column>

                <el-table-column
                  prop="audit_status"
                  label="年审状态"
                  width="150"
                  align="center"
                  :formatter="formatterAuditStatusFn"
                ></el-table-column>

                <el-table-column
                  prop="audit_time"
                  label="年审时间"
                  min-width="150"
                  align="center"
                ></el-table-column>

                <el-table-column label="操作" width="280" align="center">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="primary"
                      @click="detailFn(scope.$index, scope.row)"
                      >查看</el-button
                    >
                    <el-button
                      size="mini"
                      type="primary"
                      @click="adoptFn(scope.$index, scope.row)"
                      >通过</el-button
                    >
                    <el-button
                      size="mini"
                      type="danger"
                      @click="failFn(scope.$index, scope.row)"
                      >拒绝</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <div class="referee_certification_pagination">
                <el-pagination
                  @size-change="listSizeChange"
                  @current-change="listCurrentChange"
                  :current-page="currentPage"
                  :page-sizes="pageSizes"
                  :page-size="pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total"
                >
                </el-pagination>
              </div>
            </el-col>
          </el-row>
        </div>
        <el-row v-else>
          <el-empty description="暂无数据"></el-empty>
        </el-row>
      </div>
    </el-row>
  </div>
</template>
<style src="../../assets/css/referee/certificationList.css" scoped></style>

<script>
export default {
  name: "RefereeCertificationList",
  components: {},
  data() {
    return {
      baseAPIUrl: this.$store.state.baseAPIUrl,
      certificationListData: [], // 审核列表
      currentPage: 1, // 当前页
      pageSizes: [10, 20, 50, 100], // 可选页容量
      pageSize: 10, // 当前页容量
      total: 0, // 总条数
      auditStatusList: [
        {
          key: 1,
          value: "待年审认证",
        },
        {
          key: 2,
          value: "已完成年审认证",
        },
        {
          key: 3,
          value: "年审认证不通过",
        },
      ], // 发布状态
      levelList: [
        {
          key: 1,
          value: "三级",
        },
        {
          key: 2,
          value: "二级",
        },
        {
          key: 3,
          value: "一级",
        },
        {
          key: 4,
          value: "国家级",
        },
        {
          key: 5,
          value: "国际级",
        },
      ], // 等级列表
      checkedLevel: "", // 筛选-等级
      searchKey: "", // 搜索关键词
      loadingKey: false,
      loadingText: "",
      checkedStatus: "",
    };
  },
  created: function () {
    this.getListDataFn();
  },
  methods: {
    // 获取列表数据
    getListDataFn: function () {
      let that = this;
      let url = "/admin/refereecertification/index";
      let data = {
        page: this.currentPage,
        perPage: this.pageSize,
        keywords: this.searchKey,
        level: this.checkedLevel,
        audit_status: this.checkedStatus,
      };
      that.loadingText = "加载中...";
      that.loadingKey = true;
      this.axios
        .post(url, data)
        .then((response) => {
          that.loadingKey = false;
          if (response.code == 0) {
            that.certificationListData = response.list;
            that.total = response.total_count;
          } else {
            that.$message.error(response.msg);
          }
        })
        .catch(function (error) {
          that.loadingKey = false;
          console.log(error);
        });
    },

    // 页容量改变
    listSizeChange: function (val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.getListDataFn();
    },
    // 当前页改变
    listCurrentChange: function (val) {
      this.currentPage = val;
      this.getListDataFn();
    },
    // 等级格式化
    formatterLevelFn: function (row) {
      let level = row.level;
      let text = "";
      this.levelList.forEach(function (item) {
        if (item.key == level) {
          text = item.value;
        }
      });
      return text;
    },
    // 审核状态格式化
    formatterAuditStatusFn: function (row) {
      let status = row.audit_status;
      let text = "";
      this.auditStatusList.forEach(function (item) {
        if (item.key == status) {
          text = item.value;
        }
      });
      return text;
    },
    // 设置单元格字体颜色
    cellStyleFn: function ({ row, columnIndex }) {
      let style = "";
      if (columnIndex == 6) {
        if (row.audit_status == 2) {
          style = "color: #8ECF5F";
        } else {
          style = "color: #F6705E";
        }
      }
      return style;
    },
    // 等级筛选
    checkedLevelChange: function (val) {
      this.checkedLevel = val;
      this.currentPage = 1;
      this.getListDataFn();
    },
    // 等级筛选
    checkedStatusChange: function (val) {
      this.checkedStatus = val;
      this.currentPage = 1;
      this.getListDataFn();
    },
    // 搜索按钮
    searchFn: function () {
      this.currentPage = 1;
      this.getListDataFn();
    },
    // 查看
    detailFn: function (index, row) {
      const routeUrl = this.$router.resolve({
        name: "RefereeCertificationDetail",
      });

      window.open(routeUrl.href + "?id=" + row.id, "_blank");
    },
    // 审核通过
    adoptFn: function (index, row) {
      let that = this;
      this.$confirm("确认通过该注册申请?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        let url = "/admin/refereecertification/audit";
        let data = {
          record_id: row.id,
          status: 1,
        };
        that.loadingText = "处理中...";
        that.loadingKey = true;
        that.axios
          .post(url, data)
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 0) {
              that.$message.success("操作成功");
              that.getListDataFn();
            } else {
              that.$message.error(response.msg);
            }
          })
          .catch(function (error) {
            that.loadingKey = false;
            console.log(error);
          });
      });
    },
    // 审核不通过
    failFn: function (index, row) {
      let that = this;
      this.$confirm("确认拒绝该注册申请?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        let url = "/admin/refereecertification/audit";
        let data = {
          record_id: row.id,
          status: 2,
        };
        that.loadingText = "处理中...";
        that.loadingKey = true;
        that.axios
          .post(url, data)
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 0) {
              that.$message.success("操作成功");
              that.getListDataFn();
            } else {
              that.$message.error(response.msg);
            }
          })
          .catch(function (error) {
            that.loadingKey = false;
            console.log(error);
          });
      });
    },

    // 导出数据
    exportFn: function () {
      let that = this;
      let url = "/excel/exportReferee";
      let data = {
        keywords: this.searchKey,
        level: this.checkedLevel,
        audit_status: this.checkedStatus,
      };

      this.$confirm("是否导出名单数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        that.loadingKey = true;
        that.loadingText = "处理中...";
        that.axios
          .post(url, data, { responseType: "blob" })
          .then((response) => {
            that.loadingKey = false;
            if (response.code == 1) {
              that.$message.error(response.msg);
            } else {
              let blob = new Blob([response], {
                type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8",
              });
              const link = document.createElement("a");
              // 这里是将链接地址url转成blob地址，
              link.href = URL.createObjectURL(blob);
              // 下载文件的名称及文件类型后缀
              link.download = "裁判员注册.xlsx";
              document.body.appendChild(link);
              link.click();
              // 在资源下载完成后 清除 占用的缓存资源
              window.URL.revokeObjectURL(link.href);
              document.body.removeChild(link);
            }
          })
          .catch(function (error) {
            that.downloadBtnLoading = false;
            that.downloadBtnText = "导出名单";
            console.log(error);
          });
      });
    },
  },
};
</script>
